<?php
require_once '../config.php';

// 简单的安全检查
if (php_sapi_name() !== 'cli') {
    die('This script can only be run from command line');
}

// 验证管理员密码
// $password = readline("Enter admin password: ");
// if ($password !== ADMIN_PASSWORD) {
//     die("Invalid password\n");
// }

// 生成随机邀请码
function generateInviteCode($length = INVITE_CODE_LENGTH) {
    $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $code = '';
    for ($i = 0; $i < $length; $i++) {
        $code .= $chars[random_int(0, strlen($chars) - 1)];
    }
    return $code;
}

// 批量生成邀请码
function generateInviteCodes($count, $data) {
    try {
        $pdo = getDB();
        $stmt = $pdo->prepare("
            INSERT INTO invitations (code, name, phone) 
            VALUES (?, ?, ?)
        ");

        $successCount = 0;
        $codes = [];

        foreach ($data as $index => $person) {
            if ($index >= $count) break;

            // 生成唯一的邀请码
            do {
                $code = generateInviteCode();
                $checkStmt = $pdo->prepare("SELECT COUNT(*) FROM invitations WHERE code = ?");
                $checkStmt->execute([$code]);
            } while ($checkStmt->fetchColumn() > 0);

            try {
                $stmt->execute([
                    $code,
                    $person['name'],
                    $person['phone']
                ]);
                $successCount++;
                $codes[] = [
                    'code' => $code,
                    'name' => $person['name'],
                    'phone' => $person['phone']
                ];
            } catch (PDOException $e) {
                echo "Error generating code for {$person['name']}: " . $e->getMessage() . "\n";
            }
        }

        echo "Successfully generated $successCount invitation codes.\n\n";
        
        // 输出生成的邀请码
        echo "Generated Codes:\n";
        echo str_repeat("-", 60) . "\n";
        echo sprintf("%-15s %-20s %-15s %-30s\n", "Code", "Name", "Phone", "Invite Link");
        echo str_repeat("-", 60) . "\n";
        
        foreach ($codes as $code) {
            $inviteLink = SITE_URL . "/?code=" . $code['code'];
            echo sprintf("%-15s %-20s %-15s %-30s\n",
                $code['code'],
                $code['name'],
                $code['phone'],
                $inviteLink
            );
        }
        
        // 保存到CSV文件
        $filename = 'invite_codes_' . date('Y-m-d_His') . '.csv';
        $fp = fopen($filename, 'w');
        fputcsv($fp, ['Code', 'Name', 'Phone', 'Invite Link']);
        foreach ($codes as $code) {
            fputcsv($fp, [
                $code['code'],
                $code['name'],
                $code['phone'],
                SITE_URL . "/?code=" . $code['code']
            ]);
        }
        fclose($fp);
        
        echo "\nInvitation codes have been saved to $filename\n";

    } catch (PDOException $e) {
        echo "Database error: " . $e->getMessage() . "\n";
    }
}

// 示例数据
$inviteList = [
    ['name' => '张三', 'phone' => '13800138000'],
    ['name' => '李四', 'phone' => '13900139000'],
    ['name' => '王五', 'phone' => '13700137000'],
    // ... 添加更多邀请人信息
];

// 生成10个邀请码
generateInviteCodes(30, $inviteList); 